const path = require('path')
const TerserWebpackPlugin = require('terser-webpack-plugin')
const { merge } = require('webpack-merge')
const BaseWebpackConfig = require('./webpack.base.conf')
const HtmlWebpackPlugin = require('html-webpack-plugin')


const consoleObj = function () {
  if (process.env.NODE_ENV === "production") {
    return {
      parallel: true,
      extractComments: false,
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true
        }
      }
    };
  } else {
    return {
      parallel: true
    };
  }
};

module.exports = merge(BaseWebpackConfig, {
  mode: 'production',
  output: {
    clean: true,
    path: path.resolve(__dirname, '../dist'),
    filename: './js/[name].[chunkhash].js',
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
      filename: 'index.html',
      title: '热门精选',
      inject: 'body',
      minify: {
        collapseWhitespace: true,
        removeComments: true,
      },
    }),
  ],
  optimization: {
    minimize: true,
    minimizer: [
      new TerserWebpackPlugin(consoleObj())
    ]
  },
})
